package com.hx.dc.service.impl;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.PostConstruct;

import com.hx.dc.dao.*;
import com.hx.dc.entity.CommoDity;
import com.hx.dc.entity.RoleCatagory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.dc.service.BaseService;

public class BaseServiceImpl<T> implements BaseService<T> {

	@Autowired
	protected CommoDityMapper commoDityMapper;

	@Autowired
	protected DcCatagoryMapper dcCatagoryMapper;

	@Autowired
	protected DcEmployeeMapper dcEmployeeMapper;

	@Autowired
	protected DcRoleMapper dcRoleMapper;

	@Autowired
	protected DetailedOrderMapper detailedOrderMapper;

	@Autowired
	protected DetailedRefundsMapper detailedRefundsMapper;

	@Autowired
	protected MenuPlanMapper menuPlanMapper;

	@Autowired
	protected OrderModelMapper orderModelMapper;

	@Autowired
	protected RefundsMapper refundsMapper;

	@Autowired
	protected SimpleOrderMapper simpleOrderMapper;

	@Autowired
	protected StoreTableMapper storeTableMapper;

	@Autowired
	protected TableUserMapper tableUserMapper;

	@Autowired
	protected SysParamMapper sysParamMapper;

	@Autowired
	protected RoleCatagoryMapper roleCatagoryMapper;

	protected BaseMapper<T> baseMapper;// 没有命名空间和他的全类名对应，就没有对应的代理对象

	@PostConstruct
	// 在构造方法后，初化前执行 SysUserServiceImpl extends BaseServiceImpl aaa
	private void initBaseMapper() throws Exception {
		// 完成以下逻辑，需要对研发本身进行命名与使用规范
		// this关键字指对象本身，这里指的是调用此方法的实现类（子类）
		System.out.println("=======this :" + this);
		System.out.println("=======父类基本信息：" + this.getClass().getSuperclass());
		System.out.println("=======父类和泛型的信息："
				+ this.getClass().getGenericSuperclass());

		ParameterizedType type = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		// 获取第一个参数的class
		Class clazz = (Class) type.getActualTypeArguments()[0];
		System.out.println("=======class:" + clazz); // com.hx.ssm.entity.SysUser
														// SysUser S
														// sysUserMapper
		// 转化为属性名（相关的Mapper子类的引用名）Supplier supplierMapper
		String localField = clazz.getSimpleName().substring(0, 1).toLowerCase()
				+ clazz.getSimpleName().substring(1) + "Mapper";
		System.out.println("=======localField:" + localField);
		// getDeclaredField:可以使用于包括私有、默认、受保护、公共字段，但不包括继承的字段
		Field field = this.getClass().getSuperclass()
				.getDeclaredField(localField);
		System.out.println("=======field:" + field);
		System.out.println("=======field对应的对象:" + field.get(this));
		Field baseField = this.getClass().getSuperclass()
				.getDeclaredField("baseMapper");

		System.out.println("=======baseField:" + baseField);
		System.out.println("=======baseField对应的对象:" + baseField.get(this));
		// field.get(this)获取当前this的field字段的值。例如：Supplier对象中，baseMapper所指向的对象为其子类型SupplierMapper对象，子类型对象已被spring实例化于容器中
		baseField.set(this, field.get(this));
		System.out.println("========baseField对应的对象:" + baseMapper);

	}

	@Override
	public int insert(T t) {
		// TODO Auto-generated method stub
		int i = baseMapper.insert(t);
		return i;
	}

	@Override
	public int delete(T t) {
		// TODO Auto-generated method stub
		return baseMapper.delete(t);
	}

	@Override
	public int deleteList(List<String> keys) {
		// TODO Auto-generated method stub
		return deleteList(keys);
	}

	@Override
	public int update(T t) {
		// TODO Auto-generated method stub
		return baseMapper.update(t);
	}
	@Override
	public int updateDisable(T t){
		return baseMapper.updateDisable(t);
	}
	@Override
	public int updateAble(T t){
		return baseMapper.updateAble(t);
	}

	@Override
	public T select(T t) {
		// TODO Auto-generated method stub
		return baseMapper.select(t);
	}

	@Override
	public List<T> selectList(T t) {
		// TODO Auto-generated method stub
		return baseMapper.selectList(t);
	}

	@Override
	public List<T> search(T t) {
		// TODO Auto-generated method stub
		return baseMapper.search(t);
	}

	@Override
	public List<T> selectTime(T t) {
		// TODO Auto-generated method stub
		return baseMapper.selectTime(t);
	}

	@Override
	public List<T> selectName(T t) {
		// TODO Auto-generated method stub
		return baseMapper.selectName(t);
	}

	@Override
	public PageInfo<T> selectPage(T t, Integer pageIndex, Integer pageSize) {
		// 获取第1页，10条内容，默认查询总数count
		PageHelper.startPage(pageIndex, pageSize);
		List<T> list = baseMapper.selectPageList(t);//

		// 用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

}